package top.usts.springframework.core.convert.converter;

import cn.hutool.core.lang.Assert;

import java.util.Objects;
import java.util.Set;

/**
 * @description:
 *
 *通用转换接口
 * @author: CJ
 * @date: 2022/2/27
 */
public interface GenericConverter {

    /**Return the source and target types that this converter can convert between.***/
    Set<ConvertiblePair> getConvertibleTypes();

    /**Convert the source object to the targetType described by the {@code TypeDescriptor}.***/
    Object convert(Object source, Class sourceType, Class targetType);
    

    final class ConvertiblePair{

        private final Class<?> sourceType;

        private final Class<?> targetType;

        public ConvertiblePair(Class<?> sourceType, Class<?> targetType) {
            Assert.notNull(sourceType, "Source type must not be null");
            Assert.notNull(sourceType, "Target type must not be null");
            this.sourceType = sourceType;
            this.targetType = targetType;
        }

        public Class<?> getSourceType() {
            return sourceType;
        }

        public Class<?> getTargetType() {
            return targetType;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || ConvertiblePair.class != o.getClass()) return false;
            ConvertiblePair that = (ConvertiblePair) o;
            return Objects.equals(sourceType, that.sourceType) && Objects.equals(targetType, that.targetType);
        }

        @Override
        public int hashCode() {
            return Objects.hash(sourceType, targetType);
        }
    }


}
